//
//  exchangeCenterController.m
//  SSSAllRollAgency
//
//  Created by Darker on 2017/3/13.
//  Copyright © 2017年 Darker. All rights reserved.
//

#import "exchangeCenterController.h"
#import "benevolenceController.h"
#import "goodPointController.h"
#import "scanDetailController.h"

@interface exchangeCenterController ()
{
    
    UILabel *titleLabel;//总收益
    UILabel *nuLabel;//titleNumber
    UIButton *scanBtn;//查看
    UILabel *_numberLabel;
    UILabel *label4;
    UILabel *descLabel;
    UILabel *label3;
    UILabel *balanceLabel;
    UIButton *rowBtn;
}
@end



@implementation exchangeCenterController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.title = @"兑换中心";
    self.navigationController.navigationBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];

    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendExShandian:) name:@"sendShandian" object:nil];

    
    [self initHeaderView];
    [self reloadData];
}
- (void)sendExShandian:(NSNotification *)notification{
    
    _numberLabel.text = notification.userInfo[@"shandianNu"];
    label4.text = notification.userInfo[@"unbaleNu"];
}

- (void)reloadData{
    
   // NSString *urlStr = [NSString stringWithFormat:@"%@%@",SSSCommomUrl,@"agApi/exchange/queryMyShandian"];
    
    [[XHRequestManager manager] getRequest:kConfigPath(kMyShandian) params:nil success:^(id  _Nonnull responseObj) {
        
        XHLog (@"----response---%@",responseObj);
        if ([responseObj[@"status"] intValue] == 0 ) {
            
            //回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                
                nuLabel.text = [NSString stringWithFormat:@"%@",responseObj[@"data"][@"shanxin"]];
                
                _numberLabel.text = [NSString stringWithFormat:@"%@",responseObj[@"data"][@"shandian"]];
                _numberLabel.sd_layout
                .leftSpaceToView(balanceLabel,WidthIos6CompeteWithCurrent(10))
                .centerYEqualToView(balanceLabel)
                .heightIs(HeigthIos6CompeteWithCurrent(22))
                .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:_numberLabel.text font:_numberLabel.font height:HeigthIos6CompeteWithCurrent(22)]);
                
                rowBtn.sd_layout
                .centerYEqualToView(_numberLabel)
                .leftSpaceToView(_numberLabel,WidthIos6CompeteWithCurrent(5))
                .heightIs(HeigthIos6CompeteWithCurrent(13))
                .widthIs(WidthIos6CompeteWithCurrent(8));
                
                
                label4.text = [NSString stringWithFormat:@"%@",responseObj[@"data"][@"unable"]];
                label4.sd_layout
                .topSpaceToView(balanceLabel,HeigthIos6CompeteWithCurrent(14))
                .leftSpaceToView(label3,WidthIos6CompeteWithCurrent(20))
                .heightIs(HeigthIos6CompeteWithCurrent(20))
                .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:label4.text font:label4.font height:HeigthIos6CompeteWithCurrent(20)]);
                
            });
            
        }else{
            
            [QMUITips showError:responseObj[@"message"] inView:self.view hideAfterDelay:2];
        }

        
    } failure:^(NSError * _Nonnull error) {
        
        XHLog(@"%@",error.localizedDescription);
        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
        
    }];
    
//    [ZDNetManager zd_requestWithType:ZDHttpRequestTypeGet UrlString:kConfigPath(kMyShandian) Parameters:nil SuccessBlock:^(id response) {
//        
//        XHLog (@"----response---%@",response);
//        if ([response[@"status"] intValue] == 0 ) {
//            
//            //回到主线程刷新表格
//            dispatch_async(dispatch_get_main_queue(), ^{
//                
//                nuLabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"shanxin"]];
//                
//                _numberLabel.text = [NSString stringWithFormat:@"%@",response[@"data"][@"shandian"]];
//                _numberLabel.sd_layout
//                .leftSpaceToView(balanceLabel,WidthIos6CompeteWithCurrent(10))
//                .centerYEqualToView(balanceLabel)
//                .heightIs(HeigthIos6CompeteWithCurrent(22))
//                .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:_numberLabel.text font:_numberLabel.font height:HeigthIos6CompeteWithCurrent(22)]);
//                
//                rowBtn.sd_layout
//                .centerYEqualToView(_numberLabel)
//                .leftSpaceToView(_numberLabel,WidthIos6CompeteWithCurrent(5))
//                .heightIs(HeigthIos6CompeteWithCurrent(13))
//                .widthIs(WidthIos6CompeteWithCurrent(8));
//
//
//                label4.text = [NSString stringWithFormat:@"%@",response[@"data"][@"unable"]];
//                label4.sd_layout
//                .topSpaceToView(balanceLabel,HeigthIos6CompeteWithCurrent(14))
//                .leftSpaceToView(label3,WidthIos6CompeteWithCurrent(20))
//                .heightIs(HeigthIos6CompeteWithCurrent(20))
//                .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:label4.text font:label4.font height:HeigthIos6CompeteWithCurrent(20)]);
//
//            });
//            
//        }else{
//            
//            [QMUITips showError:response[@"message"] inView:self.view hideAfterDelay:2];
//        }
//        
//    } FailureBlock:^(NSError *error) {
//        
//        XHLog(@"%@",error.localizedDescription);
//        [QMUITips showError:error.localizedDescription inView:self.view hideAfterDelay:2];
//        
//    } progress:^(int64_t bytesProgress, int64_t totalBytesProgress) {
//        
//        
//    }];
    
}

- (void)initHeaderView{
    
    //背景
    UIImageView *headerBgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"img_bg"]];
    //headerBgView.backgroundColor = [UIColor purpleColor];
    headerBgView.userInteractionEnabled = YES;
    [self.view addSubview:headerBgView];
    headerBgView.sd_layout
    .topSpaceToView(self.view,0)
    .leftSpaceToView(self.view,0)
    .rightSpaceToView(self.view,0)
    .heightIs(HeigthIos6CompeteWithCurrent(400) - 64);
    
//    UIButton *rightNavBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [headerBgView addSubview:rightNavBtn];
//    [rightNavBtn setBackgroundImage:[UIImage imageNamed:@"icon_setting-up"] forState:UIControlStateNormal];
//    [rightNavBtn addTarget:self action:@selector(setUpBtn) forControlEvents:UIControlEventTouchDown];
//    rightNavBtn.sd_layout
//    .rightSpaceToView(headerBgView,WidthIos6CompeteWithCurrent(20))
//    .topSpaceToView(headerBgView,HeigthIos6CompeteWithCurrent(30))
//    .widthIs(WidthIos6CompeteWithCurrent(20))
//    .heightIs(HeigthIos6CompeteWithCurrent(20));
    
    titleLabel = [[UILabel alloc]init];
    [headerBgView addSubview:titleLabel];
    titleLabel.font = [SSSFont systemFontOfSize:15];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithHex:@"F5F5F5"];
    titleLabel.text = @"今日善心";
    titleLabel.sd_layout
    .centerXEqualToView(headerBgView)
    .topSpaceToView(headerBgView,HeigthIos6CompeteWithCurrent(92))
    .heightIs(HeigthIos6CompeteWithCurrent(30))
    .widthIs(WidthIos6CompeteWithCurrent(100));
    
    nuLabel = [[UILabel alloc]init];
    [headerBgView addSubview:nuLabel];
    nuLabel.font = [SSSFont systemFontOfSize:55];
    nuLabel.textAlignment = NSTextAlignmentCenter;
    nuLabel.textColor = [UIColor whiteColor];
    nuLabel.text = @"0";
    nuLabel.sd_layout
    .centerXEqualToView(headerBgView)
    .topSpaceToView(titleLabel,HeigthIos6CompeteWithCurrent(27))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:nuLabel.text font:nuLabel.font height:HeigthIos6CompeteWithCurrent(30)])
    .heightIs(HeigthIos6CompeteWithCurrent(50));
    
    //眼睛图片
    UIButton *lookBtn = [[UIButton alloc]init];
    //lookImageView.backgroundColor = [UIColor redColor];
    [lookBtn setBackgroundImage:[UIImage imageNamed:@"icon_eyes"] forState:UIControlStateNormal];
    [lookBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchDown];
    [headerBgView addSubview:lookBtn];
    lookBtn.sd_layout
    .leftSpaceToView(nuLabel,0)
    .topSpaceToView(nuLabel,0)
    .heightIs(HeigthIos6CompeteWithCurrent(12))
    .widthIs(15);
              
    
    scanBtn = [[UIButton alloc]init];
    [scanBtn setTitle:@"查看详情" forState:UIControlStateNormal];
    [scanBtn setTitleColor:[UIColor colorWithHex:@"F5F5F5"] forState:UIControlStateNormal];
    scanBtn.titleLabel.font = [UIFont systemFontOfSize:12];
    scanBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchDown];
    [headerBgView addSubview:scanBtn];
    scanBtn.sd_layout
    .centerYEqualToView(lookBtn)
    .leftSpaceToView(lookBtn,WidthIos6CompeteWithCurrent(2))
    .heightIs(HeigthIos6CompeteWithCurrent(30))
    .widthIs(WidthIos6CompeteWithCurrent(100));
    

    //横线
    UIImageView *lineV = [[UIImageView alloc]init];
    lineV.image = [UIImage imageNamed:@""];
    [headerBgView addSubview:lineV];
    lineV.sd_layout
    .centerXEqualToView(headerBgView)
    .topSpaceToView(nuLabel,HeigthIos6CompeteWithCurrent(42))
    .widthIs(WidthIos6CompeteWithCurrent(106))
    .heightIs(HeigthIos6CompeteWithCurrent(1));
    
    //我的善点余额
    balanceLabel = [[UILabel alloc]init];
    [headerBgView addSubview:balanceLabel];
    balanceLabel.font = [SSSFont systemFontOfSize:15];
    balanceLabel.textAlignment = NSTextAlignmentCenter;
    balanceLabel.textColor = [UIColor colorWithHex:@"F5F5F5"];
    balanceLabel.text = @"善点余额";
    balanceLabel.sd_layout
    .leftSpaceToView(headerBgView,WidthIos6CompeteWithCurrent(100))
    .topSpaceToView(lineV,HeigthIos6CompeteWithCurrent(20))
    .heightIs(HeigthIos6CompeteWithCurrent(15))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:balanceLabel.text font:balanceLabel.font height:HeigthIos6CompeteWithCurrent(15)]);
    
    _numberLabel = [[UILabel alloc]init];
    [headerBgView addSubview:_numberLabel];
    _numberLabel.font = [SSSFont systemFontOfSize:24];
    _numberLabel.textAlignment = NSTextAlignmentCenter;
    _numberLabel.textColor = [UIColor whiteColor];
    _numberLabel.text = @"0";
//    _numberLabel.sd_layout
//    .leftSpaceToView(balanceLabel,WidthIos6CompeteWithCurrent(10))
//    .centerYEqualToView(balanceLabel)
//    .heightIs(HeigthIos6CompeteWithCurrent(22))
//    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:_numberLabel.text font:_numberLabel.font height:HeigthIos6CompeteWithCurrent(22)]);
    
    rowBtn = [[UIButton alloc]init];
    [headerBgView addSubview:rowBtn];
    [rowBtn setBackgroundImage:[UIImage imageNamed:@"icon_arrows_right"] forState:UIControlStateNormal];
//    rowBtn.sd_layout
//    .centerYEqualToView(_numberLabel)
//    .leftSpaceToView(_numberLabel,WidthIos6CompeteWithCurrent(5))
//    .heightIs(HeigthIos6CompeteWithCurrent(13))
//    .widthIs(WidthIos6CompeteWithCurrent(8));
    
    
    label3 = [[UILabel alloc]init];
    [headerBgView addSubview:label3];
    label3.text = @"备用善点";
    label3.textColor = [UIColor colorWithHex:@"F5F5F5"];
    label3.textAlignment = NSTextAlignmentLeft;
    label3.font = [SSSFont systemFontOfSize:12];
    label3.sd_layout
    .topSpaceToView(balanceLabel,HeigthIos6CompeteWithCurrent(14))
    .leftSpaceToView(headerBgView,WidthIos6CompeteWithCurrent(136))
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:label3.text font:label3.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    
    label4 = [[UILabel alloc]init];
    [headerBgView addSubview:label4];
    label4.text = @"0";
    label4.textColor = [UIColor colorWithHex:@"F5F5F5"];
    label4.textAlignment = NSTextAlignmentLeft;
    label4.font = [SSSFont systemFontOfSize:20];
//    label4.sd_layout
//    .topSpaceToView(balanceLabel,HeigthIos6CompeteWithCurrent(14))
//    .leftSpaceToView(label3,WidthIos6CompeteWithCurrent(20))
//    .heightIs(HeigthIos6CompeteWithCurrent(20))
//    .widthIs([ZDKitManager zd_AutoSizeOfWidthWithText:label4.text font:label4.font height:HeigthIos6CompeteWithCurrent(20)]);
    
    UIButton *clickBtn = [[UIButton alloc]init];
    clickBtn.backgroundColor = [UIColor clearColor];
    [clickBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchDown];
    [headerBgView addSubview:clickBtn];
    clickBtn.sd_layout
    .leftSpaceToView(headerBgView,WidthIos6CompeteWithCurrent(170))
    .topSpaceToView(lineV,HeigthIos6CompeteWithCurrent(18))
    .widthIs(balanceLabel.frame.size.width+WidthIos6CompeteWithCurrent(10)+_numberLabel.frame.size.width+WidthIos6CompeteWithCurrent(5)+rowBtn.frame.size.width)
    .heightIs(HeigthIos6CompeteWithCurrent(22));
    
    //下方视图
    descLabel = [[UILabel alloc]init];
    descLabel.text = @"兑换说明";
    descLabel.textColor = [UIColor colorWithHex:@"40B87B"];
    descLabel.font = [SSSFont systemFontOfSize:15];
    descLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:descLabel];
    descLabel.sd_layout
    .topSpaceToView(headerBgView,HeigthIos6CompeteWithCurrent(19))
    .centerXEqualToView(self.view)
    .heightIs(HeigthIos6CompeteWithCurrent(20))
    .widthIs(WidthIos6CompeteWithCurrent(100));
    
//    //uilabel
//    UILabel *detailLabel = [[UILabel alloc]init];
//    detailLabel.text = @"1、500善心=1善点=1元\n2、善心可以兑换善点，善点不可以兑换善心\n3、单笔最多兑换50000分\n4、到账时间需要一个工作日左右\n5、兑换时间段：早上八点-晚上八点\n6、兑换过程中没有手续费";
//    detailLabel.textColor = [UIColor colorWithHex:@"454545"];
//    detailLabel.font = [SSSFont systemFontOfSize:15];
//    detailLabel.textAlignment = NSTextAlignmentLeft;
//    [self.view addSubview:detailLabel];
//    detailLabel.sd_layout
//    .topSpaceToView(descLabel,HeigthIos6CompeteWithCurrent(30))
//    .leftSpaceToView(self.view,WidthIos6CompeteWithCurrent(41))
//    .autoHeightRatio(0)
//    .widthIs(WidthIos6CompeteWithCurrent(222));
    
//    NSArray *detailArray = @[@"1、单笔兑换最多可兑换50000善点。",@"2、善点仅每月1-25日可兑换。",@"3、手续费=选择T+1到账，费率为兑换善点*0.6%；选择T+3到账，费率为兑换善点*0.3%。最低手续费为5善点。",@"4、可选到账时间：T+1（隔天到账）、T+3（3天到账）、T+7（7天到账）。",@"5、不是邮政储蓄银行卡的用户按人民银行规定收取跨行转账费率。"];
//    
//    for (int index = 0; index<detailArray.count; index++) {
//        CGFloat leftMargin = WidthIos6CompeteWithCurrent(41);
//        //CGFloat topMargin = HeigthIos6CompeteWithCurrent(10);
//        CGFloat width = WidthIos6CompeteWithCurrent(300);
//        CGFloat height = HeigthIos6CompeteWithCurrent(20);
//        UILabel *detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(leftMargin, HeigthIos6CompeteWithCurrent(390)+index*height, width, height)];
//        detailLabel.textColor = [UIColor colorWithHex:@"454545"];
//        detailLabel.text = detailArray[index];
//        detailLabel.font = [SSSFont systemFontOfSize:12];
//        detailLabel.textAlignment = NSTextAlignmentLeft;
//        [self.view addSubview:detailLabel];
//    }
    
    UILabel *label = [UILabel new];
    label.text = @"1、单笔兑换最多可兑换50000善点\n2、善点仅每月1-25日可兑换。\n3、手续费=选择T+1到账，费率为兑换善点*0.6%；选择T+3到账，费率为兑换善点*0.3%。最低手续费为5善点。\n4、可选到账时间：T+1（隔天到账）、T+3（3天到账）、T+7（7天到账）。\n5、不是邮政储蓄银行卡的用户按人民银行规定收取跨行转账费率。";
    label.font = [SSSFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentLeft;
    label.backgroundColor= [UIColor clearColor];
    label.numberOfLines = 0;
    [self.view addSubview:label];
    label.sd_layout
    .topSpaceToView(headerBgView, HeigthIos6CompeteWithCurrent(50))
    .leftSpaceToView(self.view, WidthIos6CompeteWithCurrent(41))
    .rightSpaceToView(self.view, WidthIos6CompeteWithCurrent(30))
    .bottomSpaceToView(self.view, HeigthIos6CompeteWithCurrent(150));
    
    //兑换按钮
    UIButton *exchangeBtn = [[UIButton alloc]init];
    [exchangeBtn setBackgroundColor:[UIColor colorWithHex:@"40B87B"]];
    [exchangeBtn setTitle:@"我 要 兑 换" forState:UIControlStateNormal];
    exchangeBtn.titleLabel.font = [SSSFont systemFontOfSize:18];
    [self.view addSubview:exchangeBtn];
    [exchangeBtn addTarget:self action:@selector(exchangeBtnClick) forControlEvents:UIControlEventTouchDown];
    exchangeBtn.sd_layout
    .bottomSpaceToView(self.view,HeigthIos6CompeteWithCurrent(80))
    .centerXEqualToView(self.view)
    .widthIs(WidthIos6CompeteWithCurrent(290))
    .heightIs(HeigthIos6CompeteWithCurrent(51));
    exchangeBtn.layer.masksToBounds = YES;
    exchangeBtn.layer.cornerRadius = 6;
    
}
#pragma mark - 查看善点按钮
- (void)btnClick{
    
    goodPointController *goodPointVc = [[goodPointController alloc]init];
    [self.navigationController pushViewController:goodPointVc animated:YES];
    
}
#pragma mark -兑换按钮
- (void)exchangeBtnClick{
    
    benevolenceController *benevolenVc = [[benevolenceController alloc]init];
    [self.navigationController pushViewController:benevolenVc animated:YES];
}
//查看详情
- (void)scanBtnClick{
    
    scanDetailController *scanDetailVc = [[scanDetailController alloc]init];
    [self.navigationController pushViewController:scanDetailVc animated:YES];
    
}

@end
